package com.erp.module.crm.xXCrm.wuLiao

import android.annotation.SuppressLint
import android.os.AsyncTask
import android.os.Handler
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.erp.R
import com.erp.activity.BaseActivity
import com.erp.bean.ProdyctionClientBean
import com.erp.mclass.UnCeHandler
import com.erp.module.crm.xXCrm.FragmentClientfive
import com.erp.myAdapter.WuliaoAdapter
import kotlinx.android.synthetic.main.activity_wuliao.*
import kotlinx.android.synthetic.main.header_knoya.view.*
import java.util.*

/**
 * Created by Administrator on 2016/12/30/030.
 * 作者：刘洋
 */
class WuliaoKActivity : BaseActivity(), View.OnClickListener {
    override fun onClick(v: View?) {
        when (v) {
            wuliaoHerder.iv_header_left -> {
                finish()
            }
        }
    }

    lateinit var wuliaomingxis: ArrayList<ProdyctionClientBean.wuliaomingxi>
    private var onum: String = ""
    lateinit var adapter: WuliaoAdapter
    lateinit var listgui: MutableList<String>
    lateinit var mwuliaomingxi: MutableList<ProdyctionClientBean.wuliaomingxi>
    private var all: String = ""
    private var isf = true
    override fun getLayoutId(): Int {
        activityname = a
        return R.layout.activity_wuliao
    }

    override fun initCreate() {
        initview()
        guisp()
        lvWuliao.addOnLayoutChangeListener { v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
            UnCeHandler.buzhou = "addOnLayoutChangeListener"+onum
            if (all != "not" && isf) {
                isf = false
                for (i in wuliaomingxis.indices) {
                    if (all == wuliaomingxis[i].m_code) {
                        adapter.isitem(i)
                        Handler().postDelayed({ lvWuliao.setSelection(i) }, 1000)
                        break
                    }
                }
            }
        }
    }

    private fun initview() {
        UnCeHandler.buzhou = "initview"+onum
        listgui = ArrayList()
        onum = intent.getStringExtra("shengchan")
        wuliaomingxis = ArrayList()
        mwuliaomingxi = ArrayList()
        listgui = ArrayList()
        wuliaomingxis = FragmentClientfive.getwuliaomingxi()
        all = intent.getStringExtra("all")
        adapter = WuliaoAdapter(this, mwuliaomingxi)
        lvWuliao.adapter = adapter
        lvWuliao.onItemClickListener = AdapterView.OnItemClickListener { _, _, position, _ -> adapter.isitem(position) }
        wuliaoHerder.iv_header_left.visibility = View.VISIBLE
        wuliaoHerder.iv_header_left.setOnClickListener(this)
        wuliaoHerder.tv_header_title.text = onum

    }

    private fun guisp() {
        UnCeHandler.buzhou = "guisp"+onum
        val ayncTask= @SuppressLint("StaticFieldLeak")
        object : AsyncTask<Void, Void, List<String>>() {
            override fun doInBackground(vararg params: Void): List<String>? {
                UnCeHandler.buzhou = "doInBackground"+onum
                listgui.add("全部")
                for (i in wuliaomingxis.indices) {
                    val ist = listgui.indices.none { wuliaomingxis[i].c_cnum == listgui[it] }
                    if (ist) {
                        listgui.add(wuliaomingxis[i].c_cnum)
                    }
                }
                return listgui
            }

            override fun onPostExecute(list: List<String>) {

                val shengfenadapter = ArrayAdapter(this@WuliaoKActivity, R.layout.lv_sp_tvitem, listgui)
                spGui.adapter = shengfenadapter
            }
        }
        ayncTask.execute()
        spGui.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                UnCeHandler.buzhou = "onItemSelected"+onum
                mwuliaomingxi.clear()
                for (i in wuliaomingxis.indices) {
                    if (listgui[position] == wuliaomingxis[i].c_cnum) {
                        mwuliaomingxi.add(wuliaomingxis[i])
                    } else if (listgui[position] == "全部") {
                        mwuliaomingxi.add(wuliaomingxis[i])
                    }
                }
                adapter.Allniotifi(mwuliaomingxi)
            }

            override fun onNothingSelected(parent: AdapterView<*>) = Unit
        }
    }

    companion object {
        private val a = "WuliaoActivity"
    }

}
